#include "judgecard.h"

JudgeCard::JudgeCard()
{}
QString JudgeCard::detectHandName(const QVector<int>& cards)
{
    // 点数（0~12）和花色（0~3）拆分
    QVector<int> points, suits;
    for (int c : cards)
    {
        points.append(c % 13);  // 点数 A~K -> 0~12
        suits.append(c / 13);   // 花色 ♥♦♣♠ -> 0~3
    }

    std::sort(points.begin(), points.end());  // 将点数排序便于判断顺子

    // 统计每个点数出现次数
    QMap<int, int> countMap;
    for (int p : points) countMap[p]++;

    // 判断是否为同花（所有花色相同）
    bool isFlush = std::all_of(suits.begin(), suits.end(), [&](int s) { return s == suits[0]; });

    // 判断是否为顺子（连续点数，含A-2-3-4-5特例）
    bool isStraight = true;
    for (int i = 1; i < points.size(); ++i)
    {
        if (points[i] != points[i - 1] + 1)
        {
            isStraight = false;
            break;
        }
    }
    if (!isStraight && points == QVector<int>{0, 9 ,10, 11, 12}) isStraight = true;

    // 特殊牌型识别：1314 -> 点数为1,3,1,4（即0,2,0,3）或5,2,1（4,1,0）
    if (points == QVector<int>{0, 0, 2, 3}) return isFlush ? "Flush 1314" : "1314";
    if (points == QVector<int>{0, 1, 4}) return isFlush ? "Flush 521" : "521";

    int maxCount = 0, pairCount = 0;
    for (auto cnt : countMap.values())
    {
        if (cnt == 2) ++pairCount;      // 统计对子数量
        if (cnt > maxCount) maxCount = cnt; // 最大重复数量
    }

    int n = cards.size();
    // 返回不同张数下的牌型名称

    if (n == 2)
    {
        return (maxCount == 2) ? "Pair对子" : "High Card高牌";
    } else if (n == 3)
    {
        if (maxCount == 3) return "Three of a Kind三条";
        else if (maxCount == 2) return "Pair对子";
        return "High Card高牌";
    } else if (n == 4)
    {
        if (maxCount == 4) return "Four of a Kind四条";
        else if (maxCount == 3) return "Three of a Kind三条";
        else if (pairCount == 2) return "Two Pair两对";
        else if (pairCount == 1) return "Pair对子";
        return "High Card高牌";
    } else if (n == 5)
    {
        if (isStraight && isFlush) return "Straight Flush同花顺";
        else if (maxCount == 4) return "Four of a Kind四条";
        else if (maxCount == 3 && pairCount == 1) return "Full House葫芦";
        else if (isFlush) return "Flush同花";
        else if (isStraight) return "Straight顺子";
        else if (maxCount == 3) return "Three of a Kind三条";
        else if (pairCount == 2) return "Two Pair两对";
        else if (pairCount == 1) return "Pair对子";
        return "High Card高牌";
    }

    return "Unknown";
}

// 计算牌型得分 = 筹码 × 倍率
// 筹码：点数之和（A/J/Q/K = 10，其它为点数+1）
// 倍率规则如下：同花顺8，四条7，葫芦/同花/顺子4，三条3，两对/对子2，高牌1
int JudgeCard::detectHandScore(const QVector<int>& cards)
{
    if (cards.size() < 2 || cards.size() > 5) return 0;

    QVector<int> points, suits;
    for (int c : cards)
    {
        points.append(c % 13);
        suits.append(c / 13);
    }

    std::sort(points.begin(), points.end());

    QMap<int, int> countMap;
    for (int p : points) countMap[p]++;

    bool isFlush = std::all_of(suits.begin(), suits.end(), [&](int s) { return s == suits[0]; });

    bool isStraight = true;
    for (int i = 1; i < points.size(); ++i)
    {
        if (points[i] != points[i - 1] + 1)
        {
            isStraight = false;
            break;
        }
    }
    if (!isStraight && points == QVector<int>{0, 9 ,10, 11, 12}) isStraight = true;

    int maxCount = 0, pairCount = 0;
    for (auto cnt : countMap.values())
    {
        if (cnt == 2) ++pairCount;
        if (cnt > maxCount) maxCount = cnt;
    }

    // 计算筹码（A, J, Q, K都算作10）
    int baseScore = 0;
    for (int pt : points)
    {
        if (pt == 0 || pt >= 10) baseScore += 10;
        else baseScore += pt + 1;
    }

    int n = cards.size();
    int multiplier = 1; // 默认倍率为1（高牌）

    //前两个为特殊牌型判断
    if (points == QVector<int>{0, 0, 2, 3}) multiplier = isFlush ? 8 : 5;//同花一生一世或者一生一世
    else if (points == QVector<int>{0, 1, 4}) multiplier = isFlush ? 8 : 5;//同花我爱你或者我爱你
    else if (n == 2)
    {
        multiplier = (maxCount == 2) ? 2 : 1;//对子或者高牌
    } else if (n == 3)
    {
        if (maxCount == 3) multiplier = 3;//三条
        else if (maxCount == 2) multiplier = 2;//对子
    } else if (n == 4)
    {
        if (maxCount == 4) multiplier = 7;//四条
        else if (maxCount == 3) multiplier = 3;//三条
        else if (pairCount == 2 || pairCount == 1) multiplier = 2;//两对或者对子
    } else if (n == 5)
    {
        if (isStraight && isFlush) multiplier = 8;//同花顺
        else if (maxCount == 4) multiplier = 7;//四条
        else if (maxCount == 3 && pairCount == 1) multiplier = 4;//葫芦
        else if(maxCount == 3 && pairCount == 1 && isFlush) multiplier = 9;//同花葫芦
        else if (isFlush || isStraight) multiplier = 4;//同花顺
        else if (maxCount == 3) multiplier = 3;//三条
        else if (pairCount == 2 || pairCount == 1) multiplier = 2;//两对或者对子
    }

    return baseScore * multiplier;
}
